package com.txzt.gaea.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.txzt.gaea.config.dm.DmStyle;
import org.beetl.sql.core.Interceptor;
import org.beetl.sql.core.UnderlinedNameConversion;
import org.beetl.sql.ext.DebugInterceptor;
import org.beetl.sql.ext.spring4.BeetlSqlDataSource;
import org.beetl.sql.ext.spring4.SqlManagerFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import javax.sql.DataSource;
import javax.sql.XADataSource;

/**
 * beetlSql配置类
 * <br>Created by Admin on 2017/4/27.
 * <br>星期四 at 14:51.
 */
@Configuration
@ConditionalOnMissingBean({ DataSource.class, XADataSource.class })
public class BeetlSqlConfig {


    @Primary//sqlManager工厂(核心)
    @Bean(name = "sqlManagerFactoryBean")
    public SqlManagerFactoryBean getSqlManagerFactoryBean(
            @Qualifier("datasource") DataSource datasource) {
        SqlManagerFactoryBean factory = new SqlManagerFactoryBean();
        BeetlSqlDataSource source = new BeetlSqlDataSource();
        source.setMasterSource(datasource);
        factory.setCs(source);
        //数据库类型
        factory.setDbStyle(new DmStyle());
        //debug模式 开发时使用
        factory.setInterceptors(new Interceptor[]{new DebugInterceptor()});
        //命名转换方式
        factory.setNc(new UnderlinedNameConversion());
        return factory;
    }

    @Bean(name = "txManager")//开启事务管理
    public DataSourceTransactionManager getDataSourceTransactionManager(
            @Qualifier("datasource") DataSource datasource) {
        DataSourceTransactionManager dstm = new DataSourceTransactionManager();
        dstm.setDataSource(datasource);
        return dstm;
    }

    @Bean(name = "datasource")//数据库配置bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource getDataSource(
            @Value("${spring.datasource.driver-class-name}") String driver,
            @Value("${spring.datasource.url}") String url,
            @Value("${spring.datasource.username}") String username,
            @Value("${spring.datasource.password}") String password
            //@Value("${publicKey}") String publicKey,
//            @Value("${spring.datasource.initialSize}") int initialSize,
//            @Value("${spring.datasource.minIdle}") int minIdle,
//            @Value("${spring.datasource.maxActive}") int maxActive,
//            @Value("${spring.datasource.maxWait}") long maxWait,
//            @Value("${spring.datasource.timeBetweenEvictionRunsMillis}") long timeBetweenEvictionRunsMillis,
//            @Value("${spring.datasource.minEvictableIdleTimeMillis}") long minEvictableIdleTimeMillis,
//            @Value("${spring.datasource.validationQuery}") String validationQuery,
//            @Value("${spring.datasource.testWhileIdle}") boolean testWhileIdle,
//            @Value("${spring.datasource.testOnBorrow}") boolean testOnBorrow,
//            @Value("${spring.datasource.testOnReturn}") boolean testOnReturn,
//            //@Value("${spring.datasource.poolPreparedStatements}") boolean poolPreparedStatements,
//            //@Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}") int maxPoolPreparedStatementPerConnectionSize,
//            @Value("${spring.datasource.filters}") String filters
            //@Value("${spring.datasource.connectionProperties}") String connectionProperties
    ) {
        //return DataSourceBuilder.create().build();
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(driver);
        druidDataSource.setUrl(url);
        druidDataSource.setUsername(username);
        druidDataSource.setPassword(password);
        return druidDataSource;
    }


    @Bean
    public CorsFilter corsFilter(){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", buildConfig());
        return new CorsFilter(source);
    }

    private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        return corsConfiguration;
    }
}
